package com.kyriec.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.gson.Gson;
import com.kyriec.Util.EResult;
import com.kyriec.entity.Goods;
import com.kyriec.entity.Types;
import com.kyriec.mapper.GoodsMapper;
import com.kyriec.service.GoodsService;
import com.kyriec.service.TypesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 商品(Goods)表服务实现类
 *
 * @author KyrieC
 * @since 2023-11-25 13:07:08
 */
@Service("goodsService")
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;

    @Override
    public EResult getAllGoods(Integer page, Integer pagesize) {
        Page<Goods> rowPage = new Page(page,pagesize);
        LambdaQueryWrapper<Goods> queryWrapper = new LambdaQueryWrapper<>();
        rowPage = goodsMapper.selectPage(rowPage,queryWrapper);
        return EResult.success(rowPage);
    }

    @Override
    public EResult getGoodsDetail(Integer id) {
        Goods goods = goodsMapper.selectById(id);
        return EResult.success(goods);
    }

    @Override
    public EResult getHotGoods(Integer page, Integer pagesize) {
        Page<Goods> rowPage = new Page(page,pagesize);
        LambdaQueryWrapper<Goods> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(Goods::getSales);
        rowPage = goodsMapper.selectPage(rowPage,queryWrapper);
        return EResult.success(rowPage);
    }

    @Autowired
    private TypesService typesService;

    @Override
    public EResult getIndexGoods() {
        Map<String, List<Goods>> maps = new LinkedHashMap<>();
        List<Types> typesList = typesService.findAllTypes();
        LambdaQueryWrapper<Goods> queryWrapper = new LambdaQueryWrapper();
        //根据类别，查询类别商品
        for (Types types : typesList){
            List<Goods> goodsList = goodsMapper.selectList(queryWrapper.eq(Goods::getTypeId,types.getId()));
            System.out.println(goodsList);
            //Map集合 KEY:类别对象 VALUE：该类别所有商品集合
            maps.put(new Gson().toJson(types), goodsList);
            queryWrapper = new LambdaQueryWrapper<>();
        }
        return EResult.success(new Gson().toJson(maps));
    }

    @Override
    public EResult searchGoodsByName(String goodsName) {
        LambdaQueryWrapper<Goods> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Goods::getName,goodsName).or().like(Goods::getIntro,goodsName);
        return EResult.success(goodsMapper.selectList(queryWrapper));
    }

    @Override
    public EResult getGoodsByTypeId(Integer typeId) {
        LambdaQueryWrapper<Goods> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(Goods::getTypeId,typeId);
        List<Goods> goodsList = goodsMapper.selectList(queryWrapper);
        return EResult.success(goodsList);
    }
}

